package com.xca.rxjavademo.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xca.rxjavademo.R;
import com.xca.rxjavademo.entity.Weather;
import com.xca.rxjavademo.utils.TimeUtils;

import java.text.ParseException;
import java.util.List;

public class WeatherWeekAdapter extends RecyclerView.Adapter<WeatherWeekAdapter.ViewHolder>{
    private List<Weather.Result.ForecastList> mList ;
    private Context mContext ;
    private Activity activity ;

    public WeatherWeekAdapter(List<Weather.Result.ForecastList> list , Context context , Activity activity){
        this.mList = list ;
        this.mContext = context ;
        this.activity = activity ;
    }
    public void updateData(List<Weather.Result.ForecastList> list){
        mList.clear();
        mList = list ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weather_week , parent , false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Weather.Result.ForecastList forecastList = mList.get(position);
        try {
            holder.tv1.setText(TimeUtils.extractMonthDay(forecastList.getDate()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        holder.tv2.setText(forecastList.getCondition());
        holder.tv3.setText(forecastList.getHigh_temperature() + "℃/" + forecastList.getLow_temperature() + "℃");
        holder.tv4.setText(forecastList.getWind_direction());
        holder.tv5.setText(forecastList.getWind_level());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv1 , tv2 , tv3 , tv4 , tv5 ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.wk_time);
            tv2 = itemView.findViewById(R.id.wk_weather);
            tv3 = itemView.findViewById(R.id.wk_temperature);
            tv4 = itemView.findViewById(R.id.wk_wind);
            tv5 = itemView.findViewById(R.id.wk_wind_power);
        }
    }
}
